package com.dou.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dou.yygh.hosp.repository.DepartmentRepository;
import com.dou.yygh.hosp.service.DepartmentService;
import com.dou.yygh.model.hosp.Department;
import com.dou.yygh.vo.hosp.DepartmentQueryVo;
import com.dou.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA 2021.1 x64
 *
 * @Author: 窦义超
 * @Date: 2021/08/2021/8/28/22:07
 * @Description:
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;
//上传科室接口
    @Override
    public void save(Map<String, Object> paramMap) {
        //paramMap转化为department对象
        String paramMapString = JSONObject.toJSONString(paramMap);
        Department department= JSONObject.parseObject(paramMapString,Department.class);

    Department departmentExist=  departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
    //判断
        if(departmentExist!=null){
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            departmentRepository.save(departmentExist);
        }else {
            department.setUpdateTime(new Date());
            department.setCreateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }

    }

    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //创建pageable对象 设置当前页
        //0为第一页
        Pageable pageable= PageRequest.of(page-1,limit);
        //创建example对象
        ExampleMatcher matcher =ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);
        Example<Department> example=Example.of(department,matcher);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return  all;
    }
    //删除医院接口
    @Override
    public void remove(String hoscode, String depcode) {
        Department andDepcode = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(andDepcode!=null){
            //删除
            departmentRepository.deleteById(andDepcode.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //创建list集合,用于最终封装
        List<DepartmentVo> list=new ArrayList<>();
            Department departmentQuery=new Department();
            departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);

        //根据医院编号，查询医院所有科室信息
        List<Department> departmentList= departmentRepository.findAll(example);
        //根据大科室（bigCode）分组,获取下级子科室
        Map<String, List<Department>> departmentMap =
                 departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历map集合 departmentMap
        for (Map.Entry<String,List<Department>> entry:
             departmentMap.entrySet()) {
            //大科室编号
            String bigCode = entry.getKey();
            //大科室编号对应的全部数据
            List<Department> departmentList1 = entry.getValue();

            //封装大科室
                DepartmentVo departmentVo1=new DepartmentVo();
                departmentVo1.setDepcode(bigCode);
                departmentVo1.setDepname(departmentList1.get(0).getBigname());
            //封装小科室
                    List<DepartmentVo>  children=new ArrayList<>();
            for (Department department:
                 departmentList1) {
                        DepartmentVo departmentVo2=new DepartmentVo();
                        departmentVo2.setDepcode(department.getDepcode());
                        departmentVo2.setDepname(department.getDepname());
                        //封装到list集合
                children.add(departmentVo2);
            }
            //把小科室的list集合放到大科室的children里面
            departmentVo1.setChildren(children);
                //放到最终的list里面去
            list.add(departmentVo1);
        }
        //返回
        return list;
    }

    //根据科室编号，和医院编号，查询科室名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null) {
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
